package punto_de_venta;

import java.sql.*;
import java.util.logging.*;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Mostrar_Prod.java
 *
 * Created on 24/02/2011, 08:58:33 PM
 */

/**
 *
 * @author Nekoi
 */
public class Mostrar_Prod extends javax.swing.JFrame {
    /** Creates new form Mostrar_Prod */
    static int idproducto1;
     static int idusuario1;
    static int preciop;
    static int cantidad;
    static String a2,a1,a0,a3;
    static String idusuario;
     static String idproducto;
     static int idcliente;

static String  productos [][];
    public Mostrar_Prod() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 0, 153));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        jButton1.setText("Mostrar Productos");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 500, -1, -1));

        jButton2.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18)); // NOI18N
        jButton2.setText("Comprar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 500, -1, -1));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 610, -1));

        jButton3.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        jButton3.setText("Regresar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 500, -1, -1));

        jButton4.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        jButton4.setText("Cerrar Sesion");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 540, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tempus Sans ITC", 1, 36));
        jLabel2.setText("Accesorios Petit ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tempus Sans ITC", 2, 18));
        jLabel3.setText("Nuestros Productos");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 40, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        jLabel4.setText("Para Poder realizar tu compra elige el producto");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 530, 420, 70));

        jButton5.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18)); // NOI18N
        jButton5.setText("Mis Compras");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 500, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 729, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 582, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
//        // TODO add your handling code here:
//          try{
//            n.abrirConexion();
//
//Statement sentencia;
//
//                sentencia = (Statement) Conexion.conexion.createStatement();
//
//
//           ResultSet rs = sentencia.executeQuery("SELECT * FROM productos");
//
//while (rs.next()) {
//		String s = rs.getString("idproducto");
//        System.out.println(s);
//	cont++;
//}
//           rs = sentencia.executeQuery("SELECT * FROM productos");
//            a=new String[cont][6];
//while (rs.next()) {
//		String s = rs.getString("idproducto");
//                idproducto1=Integer.parseInt(rs.getString("idproducto"));
//                System.out.println(idproducto1);
//         a[x][0]=s;
//         System.out.println(s);
//       String s1 = rs.getString("marca");
//         a[x][1]=s1;
//         System.out.println(s1);
//       String s2 = rs.getString("descripcion");
//         a[x][2]=s2;
//         System.out.println(s2);
//        String s3 = rs.getString("existencia");
//        cantidad=Integer.parseInt(rs.getString("existencia"));
//                System.out.println(cantidad);
//         a[x][3]=s3;
//         System.out.println(s3);
//       String s4 = rs.getString("preciocomp");
//         a[x][4]=s4;
//         System.out.println(s4);
//       String s5 = rs.getString("preciov");
//       preciop=Integer.parseInt(rs.getString("preciov"));
//                System.out.println(preciop);
//         a[x][5]=s5;
//         System.out.println(s5);
//         x++;
//
//}
//           cont=0;
//           x=0;
//           jTable1.setModel(new  javax.swing.table.DefaultTableModel(a, nombre));
//
//    }catch ( SQLException sqlException ) {
//    }

         int renglones=0;
            Connection cc;
            try {
                cc = DriverManager.getConnection("jdbc:mysql://localhost/petit", "root", "");
                  Statement sql=(Statement) cc.createStatement();
                  ResultSet seleccionar=sql.executeQuery("select idproducto, marca,descripcion,preciov from productos");

while(seleccionar.next()){
renglones++;
}
                  Statement sql1=(Statement) cc.createStatement();
                  ResultSet seleccionar1=sql1.executeQuery("select idproducto, marca,descripcion,preciov from productos");
                 productos=new String [renglones][4];

                 renglones=0;
while(seleccionar1.next()){
productos[renglones][0]=seleccionar1.getString("idproducto");

productos[renglones][1]=seleccionar1.getString("marca");

productos[renglones][2]=seleccionar1.getString("descripcion");
productos[renglones][3]=seleccionar1.getString("preciov");
renglones++;
}

            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            jTable1.setModel((TableModel) new javax.swing.table.DefaultTableModel(productos, nom1));

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Principal prin= new Principal();
        prin.setVisible(true);
        this.dispose();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
 idusuario1= Login.idusuario1;
///a0=Integer.toString(jTable1.getValueAt(pos, 0);
    int confirmacion=JOptionPane.showConfirmDialog(this,"Deseas Comprar el producto "+a0+"");
if(confirmacion==0){
        Connection cc;
        try {
            cc = DriverManager.getConnection("jdbc:mysql://localhost/petit", "root", "");
            Statement sql= cc.createStatement();

            ResultSet seleccionar=sql.executeQuery("select * from productos where marca='"+a1+"'");
                        System.out.println(a1);
            seleccionar.next();
            idproducto=seleccionar.getString("idproducto");
            int cantidad=Integer.parseInt(seleccionar.getString("existencia"));
//            ResultSet seleccionar1=sql.executeQuery("select idusuario from usuarios where idusuario='"+idusuario+"'");
//            seleccionar1.next();
           // idusuario=seleccionar1.getString("idusuario");
          
            if(cantidad>0){
                System.out.println(cantidad+"-"+idproducto+"-"+idusuario1);
            cantidad--;
       sql.execute("insert into ventas values(0," + idproducto + "," + idusuario1+")");
            int mod=sql.executeUpdate("Update productos set existencia="+cantidad+" where idproducto="+idproducto+"");
            JOptionPane.showMessageDialog(null,"Producto Comprado");
            }else{

            JOptionPane.showMessageDialog(null,"No Comprado...Almacen Vacio");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:

        Principal pri= new Principal();
        pri.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int pos=jTable1.getSelectedRow();
         a0= (String) jTable1.getValueAt(pos, 0);
         a1= (String) jTable1.getValueAt(pos, 1);
         a2= (String) jTable1.getValueAt(pos, 2);
        a3= (String) jTable1.getValueAt(pos, 3);
        System.out.println(a0);
    System.out.println(a1);
    System.out.println(a2);
    System.out.println(a3);


        

    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        Mi_carrito mc= new Mi_carrito();
        mc.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Mostrar_Prod().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
Conexion n=new Conexion();
static int cont=0,x=0,y=0,z=0;

static String a[][];
static String nombre[]={"","MARCA","DESCRIPCION","EXISTENCIA","PRECIO"};
static String nom1[]={"id","Marca","Descripcion","precio"};
Conexion c=new Conexion();
Validacion v=new Validacion();
}

